package com.zkdcloud.controller;

import com.xdja.interceptor.annotation.BeforeProcess;
import com.xdja.interceptor.bean.ResultBean;
import com.xdja.interceptor.exception.InvokeException;
import com.xdja.interceptor.util.ValidateUtils;
import com.zkdcloud.aspectInterceptor.advice.TokenAdvice;
import com.zkdcloud.domain.Admin;
import com.zkdcloud.domain.BusinessActivity;
import com.zkdcloud.service.BusinessActivityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

import static com.zkdcloud.constant.Constants.HTTP_CONSTANTS.HTTP_SUCCESS;
import static com.zkdcloud.constant.Constants.HTTP_CONSTANTS.OPERATOR_SUCCESS;
import static com.zkdcloud.constant.Constants.HTTP_CONSTANTS.REQUEST_ADMIN;

@RestController
@RequestMapping(value = "/mg/discount")
public class BusinessActivityController {

    @Autowired
    private BusinessActivityService businessActivityService;

    @BeforeProcess(advice = TokenAdvice.class)
    @RequestMapping(value = "/add",method = RequestMethod.POST)
    public ResultBean add(BusinessActivity businessActivity,Map<String, Object> map) throws InvokeException {
        ValidateUtils.assertCondition(!StringUtils.isEmpty(businessActivity.getDescription()),"描述不为空");
        ValidateUtils.assertCondition(!StringUtils.isEmpty(businessActivity.getName()),"名字不为空");
        ValidateUtils.assertCondition(!StringUtils.isEmpty(businessActivity.getPrice()),"价格不为空");

        Admin admin = (Admin) map.get(REQUEST_ADMIN);
        businessActivity.setBusinessId(admin.getBusinessId());

        businessActivityService.add(businessActivity);
        return ResultBean.successResult(OPERATOR_SUCCESS);
    }

    @RequestMapping(value = "/remove/{id}",method = RequestMethod.POST)
    public ResultBean remove(@PathVariable String id){
        businessActivityService.remove(id);
        return ResultBean.successResult(OPERATOR_SUCCESS);
    }

    @RequestMapping(value = "/update",method = RequestMethod.POST)
    public ResultBean update(BusinessActivity businessActivity) throws InvokeException {
        ValidateUtils.assertCondition(!StringUtils.isEmpty(businessActivity.getId()),"id不为空");
        businessActivityService.update(businessActivity);
        return ResultBean.successResult(OPERATOR_SUCCESS);
    }

    @BeforeProcess(advice = TokenAdvice.class)
    @RequestMapping(value = "/all",method = RequestMethod.GET)
    public ResultBean getAll(Map map){
        Admin admin = (Admin) map.get(REQUEST_ADMIN);
        List<BusinessActivity> businessActivities = businessActivityService.getListByBusinessId(admin.getBusinessId());
        return ResultBean.successResult(HTTP_SUCCESS,businessActivities);
    }
}
